<?php
namespace Common\Model;

use Think\Model;

class UserModel extends  CommonModel {

	public $tableName = 'user';

	public $status = [
		1 => '启用',
		2 => '禁用',
	];
	//查询单条员工
	public function get_user_info(array $params,$field="a.*"){
		$where = [];
		$params["login_name"]?$where['a.login_name'] = $params['login_name']:"";
		$params["user_id"]?$where['a.id'] = $params['user_id']:"";
		$params['status']?$where['a.status'] = $params['status']:"";

		$info = $this
		->alias("a")
		->field($field)
		->where($where)
		->find();
		return $info;

	}


	//查询员工列表
	public function get_user_list(array $params,$field="a.*"){
		$where = [];
		$pagesize = empty($params['pageSize']) ? '20' : $params['pageSize'];
		$params["login_name"]?$where['a.login_name'] = ["LIKE","%{$params['login_name']}%"]:"";
		$params['user_name']?$where['a.user_name'] = ["LIKE","%{$params['user_name']}%"]:"";
		$params["user_id"]?$where['a.id'] = $params['user_id']:"";
		$params['status']?$where['a.status'] = $params['status']:"";
		$count = $this
			->alias("a")
			->where($where)
			->count();
		$Page = new \Think\Page($count, $pagesize);

		$list = $this
			->alias("a")
			->field($field)
			->where($where)
			->order("a.id desc")
			->limit($Page->firstRow . ',' . $Page->listRows)
			->select();
		int_to_string($list,['status'=>$this->status]);
		$data = [
			'count' => $count,
			'list'  => $list
		];
		return $data;
	}



	//添加/修改员工
	public function user_save(array $params){
		$where = [
			'login_name' => $params['login_name']
		];
		if($params['user_id']){
			$where['id'] = ['neq',$params['user_id']];
		}
		$info = $this->where($where)->find();
		if($info) return $this->returnData("登录账号已存在,请重新输入");
		$data = [
			'login_name' => $params['login_name'],
			'user_name'  => $params['user_name'],
			'status'     => $params['status'],
		];
		if($params['password']) $data['password'] = md5($params['password']);
		if($params['user_id']){
			$data['update_time'] = time();
			$data['update_by'] = $params['userId'];
			$result = $this->where(['id'=>$params['user_id']])->save($data);
		}else{
			$data['create_time'] = time();
			$data['create_by'] = $params['userId'];
			$result = $this->add($data);
		}
		return $this->returnData($result?"设置成功":"设置失败",$result?200:500);



	}
}

?>
